package com.fusetech.web.flow.controller.audit;

import com.alibaba.excel.util.ListUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fusetech.common.core.controller.BaseController;
import com.fusetech.flow.audit.domain.CurrentAuditNode;
import com.fusetech.flow.audit.service.CurrentAuditNodeService;
import com.fusetech.flow.audit.service.dto.CurrentAuditNodeDTO;
import com.fusetech.flow.audit.service.mapstruct.CurrentAuditNodeWrapper;
import com.fusetech.os.core.query.QueryWrapperGenerator;
import com.fusetech.os.core.response.ResponseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 当前审核表Controller
 * @author Liuwq
 * @version 2023-05-10
 */

@Api(tags ="当前审核表")
@RestController
@RequestMapping(value = "/erp/audit/currentAuditNode")
public class CurrentAuditNodeController extends BaseController {

	@Autowired
	private CurrentAuditNodeService currentAuditNodeService;

	@Autowired
	private CurrentAuditNodeWrapper currentAuditNodeWrapperImpl;

	/**
	 * 当前审核表列表数据
	 */
	//@ApiLog("查询当前审核表列表数据")
	@ApiOperation(value = "查询当前审核表列表数据")
	//@PreAuthorize({"erp:audit:currentAuditNode:list"})
	@GetMapping("list")
	public ResponseEntity<?> list(CurrentAuditNodeDTO currentAuditNodeDTO, Page<CurrentAuditNodeDTO> page) throws Exception {
		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (currentAuditNodeDTO, CurrentAuditNodeDTO.class);
		//queryWrapper.eq("nodeUser.auditor", getUserId());//列表权限，配置节点的对应授权人可查看
		if(ObjectUtils.isNotEmpty(currentAuditNodeDTO.getAuditNode())){
			queryWrapper.like("auditNode.node_name",currentAuditNodeDTO.getAuditNode().getNodeName());
		}
		//日期
		if(ObjectUtils.isNotEmpty(currentAuditNodeDTO.getBeginCreateDate())&&ObjectUtils.isNotEmpty(currentAuditNodeDTO.getEndCreateDate())){
			queryWrapper.between("a.create_date",currentAuditNodeDTO.getBeginCreateDate(),currentAuditNodeDTO.getEndCreateDate());
		}
		queryWrapper.orderByDesc("a.create_date");
		IPage<CurrentAuditNodeDTO> result = currentAuditNodeService.findPage (page, queryWrapper);
		return ResponseMessage.OK(result);
	}

	/**
	 * 现在统计数据
	 * 统计当前用户当前审核表列表数据
	 *
	 * @param currentAuditNodeDTO 当前审计节点dto
	 * @return {@link ResponseEntity}<{@link ?}>
	 * @throws Exception 异常
	 */
	//@ApiLog("统计当前用户当前审核表列表数据")
	@ApiOperation(value = "统计当前用户当前审核表列表数据")
	//@PreAuthorize({"erp:audit:currentAuditNode:count"})
	@GetMapping("countNowData")
	public ResponseEntity<?> countNowData(CurrentAuditNodeDTO currentAuditNodeDTO) throws Exception {
		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (currentAuditNodeDTO, CurrentAuditNodeDTO.class);
		//queryWrapper.eq("nodeUser.auditor", getUserId());//列表权限，配置节点的对应授权人可查看
		long result = currentAuditNodeService.countNowData(queryWrapper);
		/*if(ObjectUtils.isEmpty(result)){
			result = 0;
		}*/
		return ResponseMessage.OK(result);
	}


	/**
	 * 根据Id获取当前审核表数据
	 */
	//@ApiLog("根据Id获取当前审核表数据")
	@ApiOperation(value = "根据Id获取当前审核表数据")
	//@PreAuthorize({"erp:audit:currentAuditNode:view","erp:audit:currentAuditNode:add","erp:audit:currentAuditNode:edit"})
	@GetMapping("queryById")
	public ResponseEntity<?> queryById(String id) {
		return ResponseMessage.OK( currentAuditNodeService.findById ( id ) );
	}

	/**
	 * 保存当前审核表
	 */
	//@ApiLog("保存当前审核表")
	@ApiOperation(value = "保存当前审核表")
	//@PreAuthorize({"erp:audit:currentAuditNode:add","erp:audit:currentAuditNode:edit"})
	@PostMapping("save")
	public ResponseEntity<?> save(@Valid @RequestBody CurrentAuditNodeDTO currentAuditNodeDTO) {
	 CurrentAuditNode currentAuditNode=currentAuditNodeWrapperImpl.toEntity (currentAuditNodeDTO);
		//新增或编辑表单保存
		if(currentAuditNodeService.saveOrUpdate(currentAuditNode)){
		    return ResponseMessage.OK(currentAuditNodeWrapperImpl.toDTO(currentAuditNode));
		}{
		    return ResponseMessage.ERROR( "保存当前审核表失败" );   
		}
	}


	/**
	 * 删除当前审核表
	 */
	//@ApiLog("删除当前审核表")
	@ApiOperation(value = "删除当前审核表")
	//@PreAuthorize({"erp:audit:currentAuditNode:del"})
	@DeleteMapping("delete")
	public ResponseEntity<?> delete(String ids) {
		String idArray[] = ids.split(",");
        currentAuditNodeService.removeByIds ( ListUtils.newArrayList ( idArray ) );
		return ResponseMessage.OK( "删除当前审核表成功" );
	}
	
	/**
     * 导出当前审核表数据
     *
     * @param currentAuditNodeDTO
     * @param page
     * @param response
     * @throws Exception
     */
    //@ApiLog("导出当前审核表数据")
    //@PreAuthorize({"erp:audit:currentAuditNode:export"})
    /*@GetMapping("export")
    public void exportFile(CurrentAuditNodeDTO currentAuditNodeDTO, Page <CurrentAuditNodeDTO> page, ExcelOptions options, HttpServletResponse response) throws Exception {
        String fileName = options.getFilename ( );
		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (currentAuditNodeDTO, CurrentAuditNodeDTO.class);
        if ( ExportMode.current.equals ( options.getMode ( ) ) ) { // 导出当前页数据
            
        } else if ( ExportMode.selected.equals ( options.getMode ( ) ) ) { // 导出选中数据
            queryWrapper.in ( "a.id", options.getSelectIds () );
        } else { // 导出全部数据
            page.setSize ( -1 );
            page.setCurrent ( 0 );
        }
        List<CurrentAuditNodeDTO> result = currentAuditNodeService.findPage ( page, queryWrapper ).getRecords ( );
        EasyExcelUtils.newInstance ( currentAuditNodeService, currentAuditNodeWrapperImpl ).exportExcel ( result,  options.getSheetName ( ), CurrentAuditNodeDTO.class, fileName,options.getExportFields (), response );
    }*/

    /**
     * 导入当前审核表数据
     *
     * @return
     */
    //@PreAuthorize({"erp:audit:currentAuditNode:import"})
    /*@PostMapping("import")
    public ResponseEntity importFile(MultipartFile file) throws IOException {
        String result = EasyExcelUtils.newInstance ( currentAuditNodeService, currentAuditNodeWrapperImpl ).importExcel ( file, CurrentAuditNodeDTO.class );
        return ResponseMessage.OK(result);
    }*/

    /**
     * 下载导入当前审核表数据模板
     *
     * @param response
     * @return
     */
    //@PreAuthorize({"erp:audit:currentAuditNode:import"})
    /*@GetMapping("import/template")
    public void importFileTemplate(HttpServletResponse response) throws IOException {
        String fileName = "当前审核表数据导入模板.xlsx";
        List<CurrentAuditNodeDTO> list = Lists.newArrayList();
        EasyExcelUtils.newInstance ( currentAuditNodeService, currentAuditNodeWrapperImpl ).exportExcel ( list,  "当前审核表数据", CurrentAuditNodeDTO.class, fileName, null, response );
    }*/

}
